package com.chuan.it_market.controller.login_controller;

import com.chuan.it_market.controller.BaseController;
import com.chuan.it_market.pojo.dto.ResponseDTO;
import com.chuan.it_market.pojo.entity.Buyer;
import com.chuan.it_market.pojo.entity.Seller;
import com.chuan.it_market.service.LoginService;
import com.chuan.it_market.service.ShopCarService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.Map;


@Controller
@RequestMapping("/login")
public class LoginController extends BaseController {

    @Resource
    LoginService loginService;
    @Resource
    ShopCarService shopCarService;

    @RequestMapping("sellerLoginPre")
    String LoginPre(){
        return "/login/seller-login";
    }
    @RequestMapping ("sellerRegisterPre")
    String RegisterPre(){return "/login/seller-register";}
    @RequestMapping("buyerLoginPre")
    String LoginPre2(){
        return "/login/buyer-login";
    }
    @RequestMapping ("buyerRegisterPre")
    String RegisterPre2(){return "/login/buyer-register";}

    //卖家登录
    @RequestMapping("sellerLogin")
    String Login(Seller seller, Model model, HttpSession session)
    {
        if(seller.getPhone()==null||seller.getPassword()==null||"".equals(seller.getPassword())){
            model.addAttribute("errorMsg","账号或密码不能为空");
            return "/login/seller-login";
        }

        Seller seller1=loginService.login(seller);

        if(seller1!=null){
            session.setAttribute("userId",seller1.getSno());
            session.setAttribute("userPassword",seller1.getPassword());
            session.setAttribute("seller1",seller1);
            return "/seller/seller-admin";

        }
        else {
            model.addAttribute("errorMsg","账号或密码错误");
            return"/login/seller-login";
        }
    }

    //卖家注册
    @RequestMapping("sellerRegister")
    @ResponseBody
    ResponseDTO Register(Seller seller, MultipartFile pic){
        if(loginService.selectSellerByPhone(seller.getPhone())!=null){
            return ResponseDTO.fail("注册失败,该手机号已经注册");
        }
        String imgUrl=uploadFile("/upload/goods/",pic);
        seller.setIcon(imgUrl);
        int res=loginService.sellerRegister(seller);
        if(res==1){
            return ResponseDTO.ok("注册成功");
        }
        else {
            return ResponseDTO.fail("注册失败");
        }
    }

    //买家注册
    @RequestMapping("buyerRegister")
    @ResponseBody
    ResponseDTO Register2(Buyer buyer, MultipartFile pic){
        if(loginService.selectBuyerByPhone(buyer.getPhone())!=null){
            return ResponseDTO.fail("注册失败,该手机号已经注册");
        }
        String imgUrl=uploadFile("/upload/goods/",pic);
        buyer.setIcon(imgUrl);
        int res=loginService.BuyerRegister(buyer);
        if(res==1){
            return ResponseDTO.ok("注册成功");
        }
        else {
            return ResponseDTO.fail("注册失败");
        }
    }

    //买家登录
    @RequestMapping("buyerLogin")
    String Login2(Buyer buyer, Model model, HttpSession session)
    {
        if(buyer.getPhone()==null||buyer.getPassword()==null||"".equals(buyer.getPassword())){
            model.addAttribute("errorMsg","账号或密码不能为空");
            return "/login/buyer-login";
        }

        Buyer buyer1=loginService.login2(buyer);

        if(buyer1!=null){
            session.setAttribute("buyer1",buyer1);
            session.setAttribute("userId",buyer1.getBno());
            session.setAttribute("userPassword",buyer1.getPassword());
            session.setAttribute("carCount",shopCarService.selectCarsCountByBno(buyer1.getBno()));


            return "/buyer/buyer-admin";
        }
        else {
            model.addAttribute("errorMsg","账号或密码错误");
            return"/login/buyer-login";
        }
    }

    //修改密码
    @RequestMapping("modifyPassword")
    @ResponseBody
    ResponseDTO modifyPassword( @RequestBody Map<String,String> param){
        String oldPassword = param.get("oldPassword");
        String newPassword = param.get("newPassword");
        String newPassword2 = param.get("newPassword2");
        if(oldPassword.isEmpty()){
            return ResponseDTO.fail("原密码不能为空！");
        }
        if(newPassword.isEmpty()){
            return ResponseDTO.fail("新密码不能为空！");
        }
        if(newPassword2.isEmpty()){
            return ResponseDTO.fail("确认密码不能为空！");
        }
        Integer sno=getUsreId();
        String realPassword =getUserPassword();
        if(!oldPassword.equals(realPassword)){
            return ResponseDTO.fail("原密码输入错误！");
        }
        if(!(newPassword.equals(newPassword2))){
            return ResponseDTO.fail("新密码输入不一致！");
        }
        loginService.updatePassword(sno,newPassword);
        return ResponseDTO.ok("成功");
    }

    @RequestMapping("modifyPassword2")
    @ResponseBody
    ResponseDTO modifyPassword2( @RequestBody Map<String,String> param){
        String oldPassword = param.get("oldPassword");
        String newPassword = param.get("newPassword");
        String newPassword2 = param.get("newPassword2");
        if(oldPassword.isEmpty()){
            return ResponseDTO.fail("原密码不能为空！");
        }
        if(newPassword.isEmpty()){
            return ResponseDTO.fail("新密码不能为空！");
        }
        if(newPassword2.isEmpty()){
            return ResponseDTO.fail("确认密码不能为空！");
        }
        Integer bno=getUsreId();
        String realPassword =getUserPassword();
        if(!oldPassword.equals(realPassword)){
            return ResponseDTO.fail("原密码输入错误！");
        }
        if(!(newPassword.equals(newPassword2))){
            return ResponseDTO.fail("新密码输入不一致！");
        }
        loginService.updatePassword2(bno,newPassword);
        return ResponseDTO.ok("密码修改成功");
    }
}
